package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.EhmInfoSrcMapper;
import com.aijk.ehm.model.EhmInfoSrc;
import com.aijk.ehm.model.EhmInfoSrcExample;
import com.aijk.ehm.model.qo.EhmInfoSrcQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EhmInfoSrcService {
    private static Logger logger = LoggerFactory.getLogger(EhmInfoSrcService.class);

    @Autowired
    private EhmInfoSrcMapper mapper;

    public EhmInfoSrcExample getExample(EhmInfoSrcQo qo) {
        EhmInfoSrcExample example = new EhmInfoSrcExample();
        EhmInfoSrcExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (qo.getTitle()!= null) criteria.andTitleLike("%"+ qo.getTitle() +"%");
            if (qo.getStatus()!= null) criteria.andStatusEqualTo(qo.getStatus());
        }
        return example;
    }

    public int count(EhmInfoSrcQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(EhmInfoSrc bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int update(EhmInfoSrc bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public EhmInfoSrc findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<EhmInfoSrc> find(EhmInfoSrcQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<EhmInfoSrc> page(PagingRequest pagingRequest, EhmInfoSrcQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<EhmInfoSrc>(mapper.selectByExample(getExample(qo)));
    }
}